//
//  Teacher.m
//  Courses
//
//  Created by xxx on 15/1/9.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "Teacher.h"
#import "Request.h"

#import "PublicCourse.h"
#import "AdvancedCourse.h"

@implementation Teacher

- (id)initWithJsonDict:(NSDictionary *)jsonDict
{
    if (self = [super init])
    {
        self.TeacherId = jsonDict[@"teacherId"];
        self.TeacherName = jsonDict[@"teacherName"];
        self.TeacherTitle = jsonDict[@"teacherTitle"];
        self.TeacherHeadPhotoUrl = jsonDict[@"teacherHeadPhotoUrl"];
        self.TeacherdescShort = jsonDict[@"teacherDescShort"];
        self.isV = jsonDict[@"isV"];
        self.teacherDescLong = jsonDict[@"teacherDescLong"];
        
//        self.TeacherId = jsonDict[@"userId"];
//        self.TeacherName = jsonDict[@"name"];
//        self.TeacherTitle = jsonDict[@"title"];
//        self.TeacherHeadPhotoUrl = jsonDict[@"headPhotoUrl"];
//        self.TeacherdescShort = jsonDict[@"descShort"];
//        self.isV = jsonDict[@"isV"];
    }
    return self;
}


- (id)initWithCourseDetails:(PublicCourseDetails *)courseDetails
{
    if (self = [super init])
    {
        self.TeacherId = courseDetails.teacherId;
        self.TeacherName = courseDetails.teacherName;
        self.TeacherTitle = courseDetails.teacherTitle;
        self.TeacherdescShort = courseDetails.teacherDescShort;
        self.TeacherHeadPhotoUrl = courseDetails.teacherHeadPhotoUrl;
    }
    return self;
}

- (id)initWithCourse:(Course *)course
{
    if (self = [super init])
    {
        self.TeacherId = course.teacherId;
        self.TeacherName = course.teacherName;
        self.TeacherTitle = course.teacherTitle;
        self.TeacherHeadPhotoUrl = course.teacherHeadPhotoUrl;
    }
    return self;
}

+ (NSArray *)getTeachers
{
    NSMutableArray *teachers = [[NSMutableArray alloc] init];
    
    Request *request = [Request requestWithName:@"get_teachers"];
    NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];
    [dict setValue:@"1" forKey:@"pageNo"];
    [dict setValue:@"20" forKey:@"pageSize"];
    request.body[@"page"] = dict;
    Response *response = [request response];
    if (response.result)
    {
        for (id jsonDict in response.content[@"teachers"])
        {
            if ([jsonDict isKindOfClass:[NSDictionary class]])
            {
                Teacher *teacher = [[self alloc] initWithJsonDict:jsonDict];
                [teachers addObject:teacher];
            }
        }
    }
    
    return [teachers copy];
}


+ (Teacher *)getTeacherDetailsById:(NSString *)teacherId
{
    if (!teacherId.length) return nil;
    
    Request *request = [Request requestWithName:@"get_teacher_detail"];
    request.body[@"teacherId"] = teacherId;
    Response *response = [request response];
    
    if (response.result)
    {
        id jsonDict = response.content[@"teacher"];
        if ([jsonDict isKindOfClass:[NSDictionary class]])
        {
            Teacher *teacher = [[self alloc] initWithJsonDict:jsonDict];
            return teacher;
        }
    }
    
    return nil;
}

+ (NSArray *)getPublicCoursesByTeacherId:(NSString *)teacherId
{
    Request *request = [Request requestWithName:@"get_teacher_open_courses"];
    request.body[@"teacherId"] = teacherId;
    Response *response = [request response];
    
    NSMutableArray *courses = [[NSMutableArray alloc] init];
    if (response.result)
    {
        for (id jsonDict in response.content[@"courses"])
        {
            if ([jsonDict isKindOfClass:[NSDictionary class]])
            {
                PublicCourse *course = [[PublicCourse alloc] initWithJsonDict:jsonDict];
                [courses addObject:course];
            }
        }
    }
    
    return [courses copy];
}

+ (NSArray *)getAdvancedCoursesByTeacherId:(NSString *)teacherId
{
    Request *request = [Request requestWithName:@"get_teacher_vip_courses"];
    request.body[@"teacherId"] = teacherId;
    Response *response = [request response];
    
    NSMutableArray *courses = [[NSMutableArray alloc] init];
    if (response.result)
    {
        for (id jsonDict in response.content[@"courses"])
        {
            if ([jsonDict isKindOfClass:[NSDictionary class]])
            {
                AdvancedCourse *course = [[AdvancedCourse alloc] initWithJsonDict:jsonDict];
                [courses addObject:course];
            }
        }
    }
    
    return [courses copy];
}

@end
